<?php
class Util
{   
    //错误信息
    protected $_errorMsg = '';
    
    /**
     * 操作提示
     *
     * @param String $msg
     * @param string $jumpUrl
     * @param int $time
     */
    public static function msgRedirect($msg,$jumpUrl='-1',$time=5)
    {
        $tpl = self::getTpl();
        $tpl->assign('msg',$msg);
        $tpl->assign('jumpUrl',$jumpUrl);
        $tpl->assign('time',$time);
        $tpl->display('msgRedirect.tpl');
        exit();
    }
    
    /**
     * 记录日志
     *
     * @param string $msg
     */
    public static function log($msg)
    {
        if( 1===ENV ){
            return true;
        }
        $dir = LOG_P.date('Ym',time());
        if(!is_dir($dir)){
            mkdir($dir);
        }
        $logFile = $dir.DIRECTORY_SEPARATOR.date('Y-m-d',time()).'.log';
        $fo = fopen($logFile,'a+');
        fwrite($fo,date('Y-m-d H:i:s',time()).' '.$msg."\r\n");
        fclose($fo);
    }
    /**
     * 获得缓存值
     *
     * @param string $cacheid
     * @return string
     */
    public static function getC($cacheid)
    {
        $cachedir = explode('.',$cacheid);
        $deep = count($cachedir);
        $dir = TMP_P.implode(DIRECTORY_SEPARATOR,array_slice($cachedir,0,$deep-2));
        $filename= $dir.DIRECTORY_SEPARATOR.$cachedir[$deep-2].'.dat';
        if(file_exists($filename)){
            $cacheAry = unserialize(file_get_contents($filename));
            return $cacheAry[$cachedir[$deep-1]];
        }else{
            return false;
        }
    }
    
    /**
     * 设置缓存
     *
     * @param string $cacheid 缓存ID
     * @param string $value 值
     */
    public static function setC($cacheid,$value)
    {
        $cachedir = explode('.',$cacheid);
        $deep = count($cachedir);
        $dir = TMP_P;
        for($i=0;$i<$deep-2;$i++){
            $dir .= DIRECTORY_SEPARATOR.$cachedir[$i];
            if(!is_dir($dir)){
                mkdir($dir);
            }
        }
        $filename = $dir.DIRECTORY_SEPARATOR.$cachedir[$deep-2].'.dat';
        if(file_exists($filename)){
            $cache = unserialize(file_get_contents($filename));
        }else{
            $cache = array();
        }
        $fp = fopen($filename,'w');
        $cache[$cachedir[$deep-1]] = $value;
        fwrite($fp,serialize($cache));
        fclose($fp);
    }
    
    public static function delC($cacheid)
    {
        $cachedir = explode('.',$cacheid);
        $deep = count($cachedir);
        $dir = TMP_P.implode(DIRECTORY_SEPARATOR,array_slice($cachedir,0,$deep-2));
        $filename = $dir.DIRECTORY_SEPARATOR.$cachedir[$deep-2].'.dat';
        if(!file_exists($filename)){
            $datFileName = $dir.DIRECTORY_SEPARATOR.$cachedir[$deep-2].DIRECTORY_SEPARATOR.$cachedir[$deep-1];
            if(file_exists($datFileName.'.dat')){
                unlink($datFileName.'.dat');
            }else{
                removeDir($datFileName);
            }
        }else{
            $cacheAry = unserialize(file_get_contents($filename));
            unset($cacheAry[$cachedir[$deep-1]]);
            $fp = fopen($filename,'w');
            fwrite($fp,serialize($cacheAry));
            fclose($fp);
        }
    }
    
    /**
     * 对ID值进行加密
     *
     * @param int $id
     * @return string
     */
    public static function id2eid($id)
    {
        return base64_encode($id);
    }
    
    /**
     * 对加密的字符串进行解密
     *
     * @param string $eid
     * 
     * @return int
     */
    public static function eid2id($eid)
    {
        return base64_decode($eid);
    }
    
    /**
     * 返回当前操作的错误，向用户返回变量$_errorMsg的值
     * 
     * @return string
     */
    public function getError()
    {
        return $this->_errorMsg;
    }
    
    public function setError()
    {
        
    }
    
    /**
     * 获得一个smarty实例
     *
     * @param array $params
     * @return obj
     */
    public static function getTpl($params=array())
    {
        if(!$params){
            $params['template_dir'] = TPL_P;
            $params['compile_dir'] = CACHE_P;
            $params['config_dir'] = CONFIG_P;
            $params['cache_dir'] = CACHE_P;
            $params['left_delimiter'] = '<{';
            $params['right_delimiter'] = '}>';
        }
        require_once(EXT_P.'ExtSmarty.php');
        return new ExtSmarty($params);
    }
    
    public static function createHtml($path,$html='')
    {
        $splite = strrpos( $path , '/' );
        $dirString = substr($path , 0 , $splite);
        $dirAry = explode( '/' , $dirString );
        $dir = '';
        if(is_array($dirAry)){
            foreach ($dirAry as $item){
                $dir = $dir.$item.DIRECTORY_SEPARATOR;
                if( !is_dir(HTML_P.$dir) ){
                    mkdir(HTML_P.$dir);
                }
            }
        }
        $filename = substr( $path , $splite+1 );
        $fp = fopen(HTML_P.$dir.DIRECTORY_SEPARATOR.$filename,'w+');
        fputs($fp,$html);
        fclose($fp);
    }
    
    public static function setCache( $path , $data )
    {
        krsort( $data , SORT_NUMERIC );
        reset( $data );
        $cache = serialize( $data );
        $fp = fopen( $path , 'w' );
        fwrite( $fp , $cache );
        fclose( $fp );
    }
    
    public static function Cache( $path , $key , $row )
    {
        $data = self::getCache( $path );
        $data[$key] = $row;
        self::setCache( $path ,$data );
    }
    
    public static function removeCache( $path , $key )
    {
        $data = self::getCache( $path );
        unset( $data[$key] );
        self::setCache( $path , $data );
    }

    public static function getCache( $path )
    {
        return file_exists( $path ) ? unserialize( file_get_contents( $path ) ) : array();
    }
}